package oop.db.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oop.data.SectionStructure;
import oop.db.Database;

public class SectionStructureDAO {

	public static List<SectionStructure> fetchById(long id) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT sstr_id, sstr_text, sstr_order, sstr_test "
					+ "FROM tblSectionStructure " 
					+ "WHERE sstr_id = ?";
	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, id);
			rs = stmt.executeQuery();
	
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<SectionStructure> fetchByTestStructure(long testStructureId)
			throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT sstr_id, sstr_text, sstr_order, sstr_test "
					+ "FROM tblSectionStructure " 
					+ "WHERE sstr_test = ? "
					+ "ORDER BY sstr_order ASC";
	
			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, testStructureId);
			rs = stmt.executeQuery();
	
			return fromResultSet(rs);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<SectionStructure> fromResultSet(ResultSet rs)
			throws SQLException {
		List<SectionStructure> sections = new ArrayList<SectionStructure>();
		while (rs.next()) {
			sections.add(new SectionStructure(rs.getLong(1), rs.getString(2), rs
					.getLong(4), rs.getInt(3)));
		}
		return sections;
	}

	public static void persist(SectionStructure section) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
				String sql = "UPDATE tblSectionStructure SET " +
						"sstr_text = ?, sstr_order = ?, sstr_test = ? " +
						"WHERE sstr_id = ?";

				conn = Database.get().getConnection();
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, section.getText());
				stmt.setInt(2, section.getOrder());
				stmt.setLong(3, section.getTestStructure().getId());
				stmt.setLong(4, section.getId());
				stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static SectionStructure create(String text, long testStructureId,
			int order) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "INSERT INTO tblSectionStructure (" +
					"sstr_text, sstr_order, sstr_test) " +
					"VALUES (?, ?, ?)";

			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, text);
			stmt.setInt(2, order);
			stmt.setLong(3, testStructureId);
			stmt.executeUpdate();
			
			rs = stmt.getGeneratedKeys();
			rs.next();
			long id = rs.getLong(1);
			
			return new SectionStructure(id, text, testStructureId, order);
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static void drop(long sectionId) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "DELETE FROM tblSectionStructure " +
					"WHERE sstr_id = ?";

			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, sectionId);
			stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}		
	}

	public static void shiftDown(long testId, long order) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			String sql = "UPDATE tblSectionStructure " +
					"SET sstr_order = sstr_order + 1 " +
					"WHERE sstr_test = ? AND sstr_order >= ?";

			conn = Database.get().getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setLong(1, testId);
			stmt.setLong(2, order);
			stmt.executeUpdate();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}		
	}

	public static void normalize(long testId) throws SQLException {
		Connection conn = null;
		CallableStatement stmt = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.prepareCall("{call spSectionStructureNormalize(?)}");
			stmt.setLong(1, testId);
			stmt.execute();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public static void moveUp(long id) throws SQLException {
		Connection conn = null;
		CallableStatement stmt = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.prepareCall("{call spSectionStructureMoveUp(?)}");
			stmt.setLong(1, id);
			stmt.execute();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}		
	}
	
	public static void moveDown(long id) throws SQLException {
		Connection conn = null;
		CallableStatement stmt = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.prepareCall("{call spSectionStructureMoveDown(?)}");
			stmt.setLong(1, id);
			stmt.execute();
		} finally {
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}		
	}
	
}
